import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;


public class MySet<T> implements Set<T>{

	private HeaderList<T> hList;
	
	public MySet(){
		hList=new MyHeaderList<T>();
	}
	
	public boolean add(T e) {
		if(hList.contains(e)){
			return false;
		}
		hList.add(e);
		return true;
	}

	public boolean addAll(Collection<? extends T> c) {
		boolean allValid=true;
		for(T val : c ){
			allValid=add(val);
		}
		return allValid;
	}

	public void clear() {
		hList.reset();		
	}

	@SuppressWarnings("unchecked")
	public boolean contains(Object o) {
		if(isEmpty()){
			return false;
		}
		return hList.contains((T) o);
	}

	public boolean containsAll(Collection<?> c) {
		boolean containsAll=true;
		
		for(Object o:c){
			containsAll=contains(o);
		}
		return containsAll;
	}

	public boolean isEmpty() {
		return hList.isEmpty();
	}

	public Iterator<T> iterator() {
		return hList.iterator();
	}

	public boolean remove(Object o) {
		if(isEmpty()){
			return false;
		}
		return hList.remove((T)o);
	}

	public boolean removeAll(Collection<?> c) {
		boolean removedAll=true;
		for(T t:this){
			removedAll=remove(t);
		}
		return removedAll;
	}

	public boolean retainAll(Collection<?> c) {
		if(c.size()==0){
			return false;
		}
		
		HeaderList<T> aux=new MyHeaderList<T>();
		
		for(Object o:c){
			if(contains(o)){
				aux.add((T)o);
			}
		}
		hList=aux;
		
		return true;
	}

	public int size() {
		int i=0;
		for(T t:hList){
			i++;
		}
		return i;
	}

	public Object[] toArray() {
		if(isEmpty()){
			return null;
		}
		Object[] arr=new Array[size()];
		int i=0;
		for(T t:hList){
			arr[i]=t;
			i++;
		}
		return arr;
	}

	public <T> T[] toArray(T[] a) {
		if(a.length<size()){
			return null;
		}/*
		int i=0;
		for(T t:hList){
			a[i]=t;
			i++;
		}
		return a;*/
		
		return (T[]) toArray();
	}

}
